/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Helpers;

import entity.Empresa;
import entity.Usuario;
import java.math.BigInteger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Carito
 */
@Stateless
public class HelloWorld_h implements HelloWorld_hLocal {

    @PersistenceContext(unitName = "SAIE-ejbPU")
    private EntityManager em;
     
     public String SignIn (String  user, String  pass) {
      
        Query q= em.createQuery("SELECT u FROM Usuario u WHERE u.nombre = :A");
        q.setParameter("A", user);
        
        Usuario u;
        try{
        u=(Usuario)q.getSingleResult();
        }catch(NoResultException e) {
        return ""+false;
         }
        if ( (u.getUsuarioPK().getIdusuario()).compareTo( new BigInteger(pass) ) == 0)
        {
            return true+"";
        }
           
       return false+"";
     }
     
     public Usuario WichUser (String  user, String  pass) {
      
        Query q= em.createQuery("SELECT u FROM Usuario u WHERE u.nombre = :A");
        q.setParameter("A", user);
        
        Usuario u;
        try{
        u=(Usuario)q.getSingleResult();
        }catch(NoResultException e) {
        return null;
         }
        if ((u.getUsuarioPK().getIdusuario()).compareTo( new BigInteger(pass) ) == 0)
        {
            return u;
        }
           
       return null;
     }
     
    @Override
     public Empresa WichEnterprise (String  id) {

        /*Query q= em.createQuery("SELECT e FROM Empresa e WHERE e.idempresa = :A");
        q.setParameter("A", new BigInteger(id));
        
        Empresa em=null;
        try{
        em=(Empresa)q.getSingleResult();
        }catch(NoResultException e) {
        return null;
        }
        
       return em*/
       return em.find(Empresa.class, new BigInteger("1"));
     }
}
